<?php

class Calendrier {

	private $months = array("1" => "Janvier", "2" => "Février", "3" => "Mars", "4" => "Avril", "5" =>"Mai", "6" => "Juin", "7" => "Juillet", "8" => "Août", "9" =>"Septembre", "10" => "Octobre", "11" => "Novembre", "12" => "Décembre");
	private $days = array("Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche");

	private $resa;
	private static $cpt;


	public function __construct() {	

	}

	public function getAllYear()
	{
		$year = date("Y");
		for($i = $year-1; $i< $year+4; $i++)
		{
			$annee[] = $i;
		}
		return $annee;
	}

	public function getCurrentYear() {
		return date("Y");
	}

	public function getCurrentMonth() 
	{
		return date("m");
	}

	public function getMonth($month) {	

		return $this->months[intval($month)];
	}

	public function getDay() {
		return $this->days;
	}

	public function getAllDay($requestedYear) {

		$result = array();

		$date = new DateTime($requestedYear."-01-01");

		while($date->format("Y") <= $requestedYear) {

			$year = $date->format("Y");
			$month = $date->format("m");
			$day = $date->format("d");

			$week = str_replace("0", "7", $date->format("w"));
			$result[$year][$month][$day] = $week;

			$date->add(new DateInterval("P1D"));
		}

		return $result;
	}



	public function getYear($allDay, $year) {
		echo count($allDay[$year]);
	}


	public function getCalendrier($index, $year, $month, $resa) {

		$cpt = 0;
		if($month < 10) {
			$m = "0".$month;
		}else {
			$m = $month;
		}

		$result = "";
		$date = new DateTime($year."-".$month."-01");

		while($date->format("m") <= $month) {

			$day = $date->format("d");

			$week = str_replace("0", "7", $date->format("w"));
			$tab[$day] = $week;

			$date->add(new DateInterval("P1D"));
		}

		$result .= '<div id="calendrier'.$index.'" class="calendrier">';
		$result .= '<table class="calendrier" id="calendrier-'.$month.'">';
		$result .= '<thead>';
		$result .= '<tr>';
		$result .= '<td class="month" colspan="7"><span>'.$this->getMonth($month).'</span></td>';
		$result .= '</td>';
		$result .= '</thead>';	
		$result .= '<tbody>';
        $result .= '<tr>'; 

      	foreach ($this->getDay() as $d) {
       	 	$result .= '<td class="listDay"><span>'.substr($d,0,3).'</span></td>';
       	}
        $result .= '</tr>';
        $result .= '<tr>';

        $end = end($tab);

		foreach($tab as $day => $positionDay) {
			if ($day == 1 && $positionDay != 01) {
                $result .= '<td colspan="'.($positionDay-1).'" class="colspan"></td>';
            }
           if($cpt < count($resa)) {
	           	if($resa[$cpt]->date == $year.'-'.$m.'-'.$day) {
	                $result .= '<td class="joursActif" title="Reservation N° '.$resa[$cpt]->id_reservation.'<br />'.$resa[$cpt]->nom_adherent.' '. $resa[$cpt]->prenom_adherent.'<br />('.$resa[$cpt]->quantite.') '.$resa[$cpt]->description.'">'.$day.'</td>';
	                //$result .= '<div class="tooltip" name='.$m.'-'.$resa[$cpt]->id_reservation.'>Reservation N° '.$resa[$cpt]->id_reservation.'</div>';
	                $cpt++;
	            } else {
                	$result .= '<td class="jours">'.$day.'</td>';
                }
	        } else {
                $result .= '<td class="jours">'.$day.'</td>';
            }
            if ($positionDay == 7 && $day != 31) {
                $result .= '</tr><tr>';
            }
        }
        if ($end != 7) {
            $result .= '<td colspan="'.(7-$end).'" class="colspan"></td>';
    
		}

		$result .= '</tr>';
        $result .= '</tbody>';
        $result .= '</table>';  
        $result .= '</div>';

		return $result;
	}
		
}	

?>